﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace He.API.Translate.Youdao
{
    public class TranslateHelper
    {
        /// <summary>
        /// 有道翻译
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static string YoudaoTranslate(string txt)
        {
            Model.webset webset = new BLL.webset().GetWebset();
            string uri = string.Format("http://fanyi.youdao.com/openapi.do?keyfrom={0}&key={1}&type=data&doctype=xml&version=1.1&q={2}", webset.youdao_keyfrom, webset.youdao_key, txt);
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(uri);
                XmlNode node = doc.SelectSingleNode("/youdao-fanyi");
                if (node.FirstChild.InnerText != "0")//返回状态不成功
                {
                    return "";
                }
                XmlNode titleNode = doc.SelectSingleNode("/youdao-fanyi/translation/paragraph");
                if (titleNode == null)
                {
                    return "";
                }
                return titleNode.FirstChild.InnerText;
            }
            catch
            {
                return "";
            }
        }
    }
}
